import { createStore, combineReducers, applyMiddleware } from './redux';
import counterReducer from './reducers/counter';
import infoReducer from './reducers/info';

import exceptionMiddleware from './middlewares/exceptionMiddleware'
import timeMiddleware from './middlewares/timeMiddleware'
import loggerMiddleware from './middlewares/loggerMiddleware'

const reducer = combineReducers({
  counter: counterReducer,
  info: infoReducer
})

// let initState = {
//   counter: {
//     count: 0
//   },
//   info: {
//     name: 'json',
//     age: 0
//   }
// }

const rewriteCreateStoreFunc = applyMiddleware(exceptionMiddleware, timeMiddleware, loggerMiddleware)

const store = createStore(reducer, {}, rewriteCreateStoreFunc)

// let store = createStore(reducer, initState) //创建 store 对象
// const store = createStore(reducer)

// const next = store.dispatch

// /* 串联中间件 */
// const logger = loggerMiddleware(store)
// const exception = exceptionMiddleware(store)
// const time = timeMiddleware(store)

// store.dispatch = exception(time(logger(next)))


store.subscribe(() => {
  let state = store.getState()
  console.log(state.counter.count, state.info.name, state.info.age)
})

store.dispatch({
  type: 'DECREMENT'
})

store.dispatch({
  type: 'SETNAME',
  name: '更改名字'
})

store.dispatch({
  type: 'SETAGE',
  age: '更改年龄 =>' + 18
})